package com.suncaper.han_yi_system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suncaper.han_yi_system.pojo.dto.ApplicationQueryDto;
import com.suncaper.han_yi_system.pojo.dto.ApplicationResultDto;
import com.suncaper.han_yi_system.pojo.entity.Application;
import com.suncaper.han_yi_system.pojo.vo.ApplicationVo;
import com.suncaper.han_yi_system.result.Result;
import com.suncaper.han_yi_system.service.ApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/application")
@CrossOrigin(origins = "*")
@Tag(name = "申请管理")
@Slf4j
public class ApplicationController {

    @Autowired
    ApplicationService applicationService;

    @Operation(summary = "新增申请信息")
    @PostMapping("/save")
    public Result save(@RequestBody Application application) {
        application.setCreateTime(LocalDateTime.now());
        if(applicationService.saveApplication(application) == 0)
            return Result.error().message("该批次已申请过,请勿重复申请!");
        return Result.ok();
    }

    @Operation(summary = "更新申请信息")
    @PutMapping("/update")
    public Result update(@RequestBody Application application) {

        application.setUpdateTime(LocalDateTime.now());
        applicationService.updateById(application);
        return Result.ok();
    }

    @Operation(summary = "删除申请信息")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        if (Objects.isNull(id)) {
            return Result.error().message("id不能为空");
        }
        applicationService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id获取申请信息")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id) {
        if (Objects.isNull(id)) {
            return Result.error().message("id不能为空");
        }
        return Result.ok().data("application", applicationService.getById(id));
    }

    @Operation(summary = "获取申请列表")
    @PostMapping("/list")
    public Result getList(@RequestBody ApplicationQueryDto applicationQueryDto) {
        Page<ApplicationVo> page = applicationService.findApplicationByQueryDto(applicationQueryDto);
        return Result.ok().data("records", page.getRecords()).data("total", page.getTotal() );
    }

    @Operation(summary = "获取用户相关申请")
    @PostMapping("/myApplication")
    public Result getMyApplication(@RequestBody ApplicationQueryDto applicationQueryDto) {
        Page<ApplicationVo> myApplication = applicationService.findMyApplication(applicationQueryDto);
        return Result.ok().data("records", myApplication.getRecords()).data("total", myApplication.getTotal());
    }

    @Operation(summary = "审核通过")
    @PostMapping("/pass")
    public Result pass(@RequestBody ApplicationResultDto applicationResultDto) {
        applicationResultDto.setResult(Byte.valueOf("1"));
        applicationService.resultApplication(applicationResultDto);
        return Result.ok();
    }

    @Operation(summary = "审核拒绝")
    @PostMapping("/rejected")
    public Result reject(@RequestBody ApplicationResultDto applicationResultDto) {
        applicationResultDto.setResult(Byte.valueOf("2"));
        applicationService.resultApplication(applicationResultDto);
        return Result.ok();
    }

    @Operation(summary = "提交审核")
    @PostMapping("/submit")
    public Result submit(@RequestBody List<Long> ids) {
        applicationService.submitApplication(ids);
        return Result.ok();
    }
}